package com.sky.common;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.writer.ObjectWriter;

import java.lang.reflect.Type;

public class EnumSerializer<E extends BaseEnum> implements ObjectWriter<E> {
    @Override
    public void write(JSONWriter jsonWriter, Object o, Object o1, Type type, long l) {
        if (o == null) {
            jsonWriter.writeNull();
            return;
        }
        // 假设枚举类有 getCode() 方法
        if (o instanceof BaseEnum) {
            BaseEnum bo = (BaseEnum) o;
            jsonWriter.writeInt32(bo.getCode());
        } else {
            // 默认使用枚举的name()
            jsonWriter.writeString(o.toString());
        }

    }
}
